<?php

$cache_time=10;
$OJ_CACHE_SHARE=false;
$view_title= "题库-评测详情";
require_once("config/path_config.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once('./common/result_id.php');
require_once("./include/const.inc.php");
require_once("./config/privliege_config.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."front/model/down_log_model.php");

function getResultDes($resultID) {
    $resultDesc = '--';
    switch( $resultID ) {
        case ResultID::ID_AC:
            $resultDesc = '<span style="color:green;">正确(AC)</span>';
            break;
        case ResultID::ID_PE:
            $resultDesc = '<span style="color:red;">格式错误(PE)</span>';
            break;
        case ResultID::ID_WA:
            $resultDesc = '<span style="color:red;">答案错误(WA)</span>';
            break;
        case ResultID::ID_TLE:
            $resultDesc = '<span style="color:orange;">时间超限(TLE)</span>';
            break;
        case ResultID::ID_MLE:
            $resultDesc = '<span style="color:orange;">内存超限(MLE)</span>';
            break;
        case ResultID::ID_OLE:
            $resultDesc = '<span style="color:orange;">输出超限(OLE)</span>';
            break;
        case ResultID::ID_RE:
            $resultDesc = '<span style="color:orangered;">运行错误(RE)</span>';
            break;
        case ResultID::ID_CE:
            $resultDesc = '<span style="color:orangered;">编译错误(CE)</span>';
            break;
    }
    return $resultDesc;
}

// 判断输入参数是否正确
if ( !isset($_GET['sid']) ){
	echo "未查询到相关信息!\n";
	require_once("oj-footer.php");
	exit(0);
}

// 查询指定ID的提交信息
$id = intval($_GET['sid']);
$sql="SELECT * FROM `solution` WHERE `solution_id`=?";
$result=pdo_query($sql, $id);
$row = null;
if ( $result ) {
    $row=$result[0];
}
if ( ! isset($row) ) {
	echo "未查询到相关信息!\n";
	require_once("oj-footer.php");
	exit(0);
}
// 题目ID
$problem_id = $row['problem_id'];

// 是否可以获取程序运行详情
//1. 当前用户是source_browser
$isAllowGetRTInfo = isset($_SESSION['teacher']) && $_SESSION['teacher'];
//2. 当前用户是管理员
$isAllowGetRTInfo |= isset($_SESSION['administrator']) && $_SESSION['administrator'];
// 3. 系统配置允许查看评测详情
$isAllowGetRTInfo |= isset($_SESSION['user_id']) && PrivliegeConfig::$IS_ALLOW_SEE_JUDGE_DETAIL;

if ( ! $isAllowGetRTInfo ) {
    $view_errors= "抱歉,你没有查看该信息的权限!";
    require("template/bs3/error.php");
    exit(0);
}

// 测试样例总数和通过总数
$totalTestCnt = $row['num_of_test'];
$passTestCnt = $row['num_of_pass'];
$total_score = '--';
if ( $totalTestCnt > 0 ) {
    $total_score = intval($passTestCnt * 100 / $totalTestCnt);
}

// 更新评测错误的记录
$testFileUrl = "/front/view/test_down_view.php?problemid=".$problem_id."&filename=";

$sql="SELECT * FROM `solution_detail` WHERE `solution_id`=?";
$rows=pdo_query($sql, $id);
$judgeDetailList = array();
foreach ( $rows as $row) {
    $oneDetail = array();
    $filename = $row['file_name'];
    $filename = str_replace(strrchr($filename, "."),"",$filename);
    // 评测记录
    $oneDetail["result"] = getResultDes(intval($row['result']));
    $oneDetail["filename"] = $filename;
    $oneDetail["inputfile"] = $testFileUrl.$filename.".in";
    $oneDetail["outputfile"] = $testFileUrl.$filename.".out";
    array_push($judgeDetailList, $oneDetail);
}

// 样例文件信息
$max_down_cnt = SysConfig::$MAX_DOWN_CNT;
$left_down_cnt = (new DownLogModel())->checkAndGetLeftDownCnt($_SESSION['user_id']);

// 错误详情
$view_reinfo="";
// 运行错误,不用查询运行详情
if ( $row['result']==ResultID::ID_RE ) {
    // 运行错误
    $view_reinfo = "程序运行错误,请确认指针使用是否存在异常,数组访问是否越界";
}
else {
	$sql="SELECT `error` FROM `runtimeinfo` WHERE `solution_id`=?";
    $result=pdo_query($sql, $id);
    $row = null;
    if ( $result ) {
        $row=$result[0];
    }

    if( $row ) {
    	$view_reinfo= htmlentities(str_replace("\n\r","\n",$row['error']),ENT_QUOTES,"UTF-8");
    }
}

/////////////////////////Template
require("template/bs3/reinfo.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');
?>

